/*
 * Copyright 2003 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easyaop.commons.asm;


import java.lang.invoke.MethodType;
import java.lang.reflect.Method;

import org.objectweb.asm.Type;

/**
 * A representation of a method signature, containing the method name,
 * return type, and parameter types.
 */
public class Signature {
    private final String name;

    private final String desc;

    private final Type returnType;

    private final Type[] argumentTypes;

    private final int argumentCount;

    public Signature(String name, String desc) {
        // TODO: better error checking
        if (name.indexOf('(') >= 0) {
            throw new IllegalArgumentException("Name '" + name + "' is invalid");
        }
        this.name = name;
        this.desc = desc;
        this.returnType = Type.getReturnType(desc);
        this.argumentTypes = Type.getArgumentTypes(desc);
        this.argumentCount = argumentTypes.length;
    }

    public Signature(String name, Type returnType, Type[] argumentTypes) {
        this(name, Type.getMethodDescriptor(returnType, argumentTypes));
    }

    public Signature(String name, MethodType methodType) {
        this(name, methodType.toMethodDescriptorString());
    }

    public Signature(Method method) {
        this(method.getName(), Type.getMethodDescriptor(method));
    }

    public String getName() {
        return name;
    }

    public String getDescriptor() {
        return desc;
    }

    public Type getReturnType() {
        return returnType;
    }

    public Type[] getArgumentTypes() {
        return argumentTypes;
    }

    public int getArgumentCount() {
        return argumentCount;
    }

    public Signature insertArgumentTypes(int index, Type type) {
        int len = argumentCount;
        if (len == 0) {
            return new Signature(name, Type.getMethodDescriptor(returnType, type));
        }
        if (index < 0 || index >= len) {
            throw new IllegalArgumentException(index + " out of bounds");
        }
        Type[] argumentTypes = this.argumentTypes;
        Type[] arr = new Type[len + 1];
        System.arraycopy(argumentTypes, 0, arr, 0, index);
        arr[index] = type;
        System.arraycopy(argumentTypes, index, arr, index + 1, len - index);
        return new Signature(name, Type.getMethodDescriptor(returnType, arr));
    }

    public Signature changeName(String name) {
        return new Signature(name, desc);
    }

    public MethodType getMethodType() {
        return MethodType.fromMethodDescriptorString(desc, Thread.currentThread().getContextClassLoader());
    }

    public String toString() {
        return name + desc;
    }

    public boolean equals(Object o) {
        if (o == null)
            return false;
        if (!(o instanceof Signature))
            return false;
        Signature other = (Signature) o;
        return name.equals(other.name) && desc.equals(other.desc);
    }

    public int hashCode() {
        return name.hashCode() ^ desc.hashCode();
    }
}
